package xyz.icxl.kotlion.ext

import com.blankj.utilcode.util.LanguageUtils
import com.blankj.utilcode.util.StringUtils
import java.text.SimpleDateFormat
import java.util.*

private val calendar = Calendar.getInstance()

/**
 * 国际化的月
 */
val Date.i18nMonth: String
    get() = String.format(LanguageUtils.getSystemLanguage(), "%tb", this)

/**
 * 国际化的月 非缩写
 */
val Date.i18nMonth2: String
    get() = String.format(LanguageUtils.getSystemLanguage(), "%tB", this)


/**
 * 国际化的星期
 */
val Date.i18nWeek: String
    get() {
        calendar.time = this
        return when (calendar.get(Calendar.DAY_OF_WEEK)) {
            Calendar.MONDAY -> StringUtils.getString(R.string.week_1)
            Calendar.TUESDAY -> StringUtils.getString(R.string.week_2)
            Calendar.WEDNESDAY -> StringUtils.getString(R.string.week_3)
            Calendar.THURSDAY -> StringUtils.getString(R.string.week_4)
            Calendar.FRIDAY -> StringUtils.getString(R.string.week_5)
            Calendar.SATURDAY -> StringUtils.getString(R.string.week_6)
            Calendar.SUNDAY -> StringUtils.getString(R.string.week_7)
            else -> StringUtils.getString(R.string.week_7)
        }
    }


/**
 * 国际化的天
 */
val Date.i18nDay: String
    get() {
        calendar.time = this
        return calendar.get(Calendar.DAY_OF_MONTH).toString()
    }

/**
 * 国际化天和星期的组合
 * 返回例子：8 周日 、8 Sun
 */
val Date.i18nDayWeek: String
    get() {
        calendar.time = this
        return calendar.get(Calendar.DAY_OF_MONTH).toString() + " " + i18nWeek
    }

private val ymd: SimpleDateFormat = SimpleDateFormat("yyyyMMdd")

/**
 * 比较大小 只比较年月日
 * @return 当前时间大于[date] 返回 1；当前时间小于[date] 返回-1； 当前等于[date]返回0
 */
fun Date.compareTo2(date: Date): Int {
    val dateFirst = ymd.format(this);
    val dateLast = ymd.format(date);
    val dateFirstIntVal = Integer.parseInt(dateFirst)
    val dateLastIntVal = Integer.parseInt(dateLast)
    if (dateFirstIntVal > dateLastIntVal) {
        return 1
    } else if (dateFirstIntVal < dateLastIntVal) {
        return -1
    }
    return 0
}